﻿// --------------------------------------------------------------------------------------------------------------------
// <copyright file="AddressActioner.cs" company="DissingNelleman">
//   
// All rights reserved. </copyright>
// <summary>
//   The address actioner.
// </summary>
// --------------------------------------------------------------------------------------------------------------------

namespace ServiceLibrary.Actioners.Locations
{
    using System;
    using System.Collections.Generic;

    using NHibernate;

    using ServiceLibrary.Models.Domain;
    using ServiceLibrary.Services.Commands;
    using ServiceLibrary.Services.Queries;
    using ServiceLibrary.Services.Results;

    /// <summary>
    /// The address actioner.
    /// </summary>
    public class AddressActioner : ActionerBase
    {
        #region Constructors and Destructors

        /// <summary>
        /// Initializes a new instance of the <see cref="AddressActioner"/> class.
        /// </summary>
        /// <param name="session">
        /// The session.
        /// </param>
        /// <param name="activeUser">
        /// The active user.
        /// </param>
        public AddressActioner(ISession session, User activeUser)
            : base(session, activeUser)
        {
        }

        #endregion

        #region Public Methods and Operators

        /// <summary>
        /// The create address.
        /// </summary>
        /// <param name="address">
        /// The address.
        /// </param>
        public void CreateAddress(Address address)
        {
            address.CreatedDateTime = DateTime.Now;
            this.Session.Save(address);

            // log.CreateLogEntry(where.ID, String.Format("Adresse '{0} {1}, {2}', blev oprettet.", where.StreetName, where.StreetNr, where.City), 
            // Enums.LogEntryType.Address);
        }

        /// <summary>
        /// The execute search query.
        /// </summary>
        /// <param name="query">
        /// The query.
        /// </param>
        /// <returns>
        /// The <see cref="AddressResult"/>.
        /// </returns>
        public AddressResult ExecuteSearchQuery(AddressQuery query)
        {
            SearchSettings settings = query.Settings;

            var result = new AddressResult();

            IQueryOver<Address, Address> search = this.Session.QueryOver<Address>();
            this.ApplyDefaultCriterias(settings, search);

            result.TotalRowCount = search.RowCount();

            IList<Address> addresses = ApplySkipAndTake(settings, search);

            foreach (Address address in addresses)
            {
                Services.Dto.Address dto = Services.Dto.Address.Convert(address);
                if (dto != null)
                {
                    result.List.Add(dto);
                }
            }

            return result;
        }

        /// <summary>
        /// The get address.
        /// </summary>
        /// <param name="id">
        /// The id.
        /// </param>
        /// <returns>
        /// The <see cref="Address"/>.
        /// </returns>
        public Address GetAddress(Guid id)
        {
            var where = this.Session.Get<Address>(id);

            return where;
        }

        #endregion

        #region Methods

        /// <summary>
        /// The create address.
        /// </summary>
        /// <param name="cmd">
        /// The cmd.
        /// </param>
        internal void CreateAddress(CreateAddressCommand cmd)
        {
            if (cmd.Dto != null)
            {
                var addr = new Address
                               {
                                   ID = cmd.Dto.ID, 
                                   Att = cmd.Dto.Att, 
                                   City = cmd.Dto.City, 
                                   StreetName = cmd.Dto.StreetName, 
                                   StreetNr = cmd.Dto.StreetNr, 
                                   Zip = cmd.Dto.Zip
                               };
                CreateAddress(addr);
            }
        }

        /// <summary>
        /// The delete address.
        /// </summary>
        /// <param name="cmd">
        /// The cmd.
        /// </param>
        internal void DeleteAddress(DeleteAddressCommand cmd)
        {
            var addr = this.Session.Get<Address>(cmd.Dto);

            if (addr != null)
            {
                int addressUsage = this.Session.QueryOver<Location>().Where(x => x.Address.ID == addr.ID).RowCount();
                if (addressUsage == 0)
                {
                    this.Session.Delete(addr);
                }
            }
        }

        /// <summary>
        /// The update address.
        /// </summary>
        /// <param name="cmd">
        /// The cmd.
        /// </param>
        internal void UpdateAddress(UpdateAddressCommand cmd)
        {
            if (cmd.Dto != null)
            {
                var addr = this.Session.Get<Address>(cmd.Dto.ID);
                if (addr != null)
                {
                    addr.ID = cmd.Dto.ID;
                    addr.Att = cmd.Dto.Att;
                    addr.City = cmd.Dto.City;
                    addr.StreetName = cmd.Dto.StreetName;
                    addr.StreetNr = cmd.Dto.StreetNr;
                    addr.Zip = cmd.Dto.Zip;
                }
            }
        }

        #endregion
    }
}